/**
 * Created by hxpeng on 2017/5/18.
 */
(function () {
    'use strict';

    angular.module('App.pages.member')
        .controller('AlbumCtrl', AlbumCtrl);

    function AlbumCtrl($scope,$state,MemberModel,$location,$http,Global,$filter,LoginAccountAuth,AppUtils) {
        var currentPath = $location.url();
        if(currentPath.indexOf("#") != -1){
            currentPath = currentPath.split('#')[0];
        }
        currentPath = Global.path.wechat_path + currentPath;
        $http.post(Global.path.server_sdk_auth_path,{"url":currentPath})
            .success(function(data){
                wx.config({
                    debug: false,
                    appId: data.appId,
                    timestamp: data.timestamp,
                    nonceStr: data.noncestr,
                    signature: data.signature,
                    jsApiList: ['chooseImage','previewImage']
                });
                wx.error(function(res){
                    console.log(res);

                });
            })
            .error(function(data,header,config,status){
                alert("请求微信sdk校验异常");
            });


        $scope.photo = function(){
            $state.go("memberMenu.upload");
        }
        var vm = this;
        vm.loginAccount = {};
        vm.memberIds = [];
        vm.exhortaions = [];
        vm.loadMoreBtnStatus = true;
        vm.loadMoreBtnStr = "加载更多";
        vm.pageIndex = 1;//第一页
        vm.imgPre = Global.path.img_path;

        LoginAccountAuth.getAccount().then(function(data){
            vm.loginAccount = data;
            angular.forEach(vm.loginAccount.studentVoList,function(obj){
                vm.memberIds.push(obj.memberId);
            })
            vm.init();
        })



        $scope.loadMoreActivity = function(){
            if(vm.loadMoreBtnStatus){
                vm.pageIndex = vm.pageIndex + 1;
                vm.init();
            }
        }

        //大图片预览
        $scope.previewImg = function(url,key){
            var imgArray = [];
            angular.forEach($scope.resultArray, function(data) {
                if(data.key == key){
                    angular.forEach(data.timeArray, function(val) {
                        imgArray.push(val.url);
                    });
                }
            });
            wx.previewImage({
                current: url,
                urls: imgArray
            });
        }


        $scope.resultArray = [];

        vm.init = function(){
            AppUtils.loading();
            MemberModel.queryStudentsPhoto({
                memberIds: vm.memberIds,
                pageInfo: {
                    pageSize: 5,
                    pageNumber: vm.pageIndex
                }
            }).$promise.then(function(response){
                var reponseDate = response.classPhoto;
                if(AppUtils.isUndefinedOrNull(reponseDate)){
                    vm.loadMoreBtnStatus = false;
                    vm.loadMoreBtnStr = "没有更多数据了";
                    return;
                }
                var items = reponseDate.pageResult;
                var pageIndex = reponseDate.pageIndex;
                var pageTotalPageSize = reponseDate.pageTotalPageSize;
                if(pageIndex == pageTotalPageSize || items.length == 0){
                    vm.loadMoreBtnStatus = false;
                    vm.loadMoreBtnStr = "没有更多数据了";
                }

                var repeat = [];
                angular.forEach (items,function(data,index,array){
                    var keepRun = true;
                    var key = $filter("date")(new Date(data.createDate), "yyyy-MM-dd");
                    angular.forEach(repeat,function(i){
                        if(key == i){
                            keepRun = false;
                        }
                    });

                    if(keepRun){
                        var obj = {};
                        obj.key = key;
                        var timeArray = [];
                        angular.forEach (items,function(object,k){
                            var time = $filter("date")(new Date(object.createDate), "yyyy-MM-dd");
                            if(key == time){
                                var imgObj = {};
                                imgObj.url = object.filePath;
                                imgObj.key = key;
                                timeArray.push(imgObj);
                            }
                        })
                        obj.timeArray = timeArray;
                        $scope.resultArray.push(obj);
                        repeat.push(key);
                        AppUtils.loadfinish();
                    }
                })
            })
        }

        //悬浮菜单
        $("#hamburgerMenuBtn").on("click",function () {
            $("#hamburgerMenuBtn").toggleClass("active");
        });

        var lastTop = 0;
        $scope.scrollFun = function(){
            var top = $ionicScrollDelegate.$getByHandle('albumScroll').getScrollPosition().top;
            if(top > 200 && top > lastTop){
                $("#hamburgerMenuBtn").removeClass("active");
                $("#hamburgerMenuBtn").css("right","-80px")
            }else if(lastTop - top > 20){
                $("#hamburgerMenuBtn").css("right","40px")
            }
            lastTop = top;
        }

    }
})();
